<!--学员积分-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="搜索学员" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学员姓名/手机号"
          clearable
          style="width: 240px"
          @keyup.enter="getList"
        >
          <template #append>
            <el-button :icon="Search" @click="getList" />
          </template>
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="所在班级" prop="classId">
        <el-select
          v-model="queryParams.classId"
          placeholder="请选择所在班级"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in classList"
            :key="item.id"
             :label="item.beOver === '0' ? item.name + '(已结业)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报课课程" prop="courseId">
        <el-select
          v-model="queryParams.courseId"
          placeholder="请选择报课课程"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.state === '1' ? item.name + '(已停用)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班级老师" prop="teacher">
        <el-select
          v-model="queryParams.teacher"
          placeholder="请选择班级老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="
                !item.isJob
                  ? item.label + '(已离职)'
                  : !item.isTeacher
                  ? item.label + '(非授课)'
                  : item.label
              "
            :value="item.value"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item label="学员类型" prop="stage">
        <el-select
          v-model="queryParams.stage"
          placeholder="请选择学员类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="stage in stageList"
            :key="stage.value"
            :label="stage.label"
            :value="stage.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="getList"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button plain icon="Download" @click="handleExport()">
          导出
        </el-button>
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="学员姓名" align="center" prop="studentName">
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">
            {{ scope.row.studentName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="学员类型" align="center" prop="stage" />
      <el-table-column label="积分余额" align="center" prop="totalScore" />
      <el-table-column label="累计加分" align="center" prop="addScore" />
      <el-table-column label="累计减分" align="center" prop="subScore" />
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleGrant(scope.row)"
            v-hasPermi="['marketing:points:add']"
          >
            发放积分
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
      size="small"
    />
  </div>
  <GrantDialog ref="gdRef" @refreshPage="getList" />
  <el-drawer
    v-model="open"
    title="积分详情"
    :width-header="true"
    size="80%"
    :close-on-click-modal="false"
    :before-close="
      () => {
        getList();
        open = false;
      }
    "
  >
    <GrantByPer v-if="open" :studentId="currentItem.studentId" />
  </el-drawer>
</template>

<script setup>
import { getAllPointListAPI } from "@/api/marketing/points";
import { getClassListAPI } from "@/api/registrar/class";
import { getCourseListAPI } from "@/api/registrar/course";
import { getPeopleList } from "@/api/studentManagement/index";
import { Search } from "@element-plus/icons-vue";
import GrantDialog from "./grantDialog.vue";
import GrantByPer from "./grantByPer.vue";

const { proxy } = getCurrentInstance();

// 学员类型：1线索学员 2在学学员 3跟进学员 4报读
const stageList = [
  { value: "2", label: "在学学员" },
  { value: "1", label: "线索学员" },
  { value: "3", label: "跟进学员" },
  { value: "3", label: "报读学员" },
];

const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const open = ref(false);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    // classId: null,
    // teacher: null,
    // courseId: null,
    stage: null,
  },
  list: [],
  currentItem: {},
});

const { queryParams, list, currentItem } = toRefs(data);

const classList = ref([]);
const teacherList = ref([]);
const courseList = ref([]);

const gdRef = ref(null);

// 事件------------------------------------------------------------------------------------------------------------

// 查看详情
function handleDetail(row) {
  currentItem.value = row;
  open.value = true;
}
// 发放积分
function handleGrant(row) {
  gdRef.value.handleDialogOpen(row.studentId);
}

function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    name: null,
    stage: null,
  };

  getList();
}

// 方法------------------------------------------------------------------------------------------

// 课程
function getCourseList() {
  getCourseListAPI({
    pageSize: 9999,
    pageNum: 1,
  }).then((response) => {
    courseList.value = response.rows;
    loading.value = false;
  });
}

// 班级
function getClassList() {
  loading.value = true;
  getClassListAPI({ pageSize: 9999, pageNum: 1 }).then((response) => {
    classList.value = response.rows;
    loading.value = false;
  });
}

// 教师
function getTeacherList() {
  loading.value = true;
  getPeopleList({ isTeach: 1 }).then((response) => {
    teacherList.value = response.data;
    loading.value = false;
  });
}

function initPicker() {
  getCourseList();
  getClassList();
  getTeacherList();
}

function getList() {
  loading.value = true;
  getAllPointListAPI({ ...queryParams.value }).then((response) => {
    list.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 调用-------------------------------------------------------------------------------------------------------------------
//initPicker();
getList();
</script>

<style lang="scss" scoped></style>
